package com.it.controller;


import com.alibaba.fastjson.JSONObject;

import com.it.common.Result;
import com.it.domain.User;
import com.it.service.LoginService;
import com.it.service.UserService;
import com.it.common.CONSTS;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/User")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private LoginService loginService;


    @PostMapping("/AddUser")
    public Object AddUser(@RequestBody User user){

        String username = user.getUsername();
        String password = user.getPassword();
        String introduce = user.getIntroduce().trim();
        String image = user.getImage();
        String phone = user.getPhone();
        String email = user.getEmail();
        Date createTime = user.getCreateTime();
        Byte sex = user.getSex();

        User user1 = new User();
        user1.setImage(image);
        user1.setEmail(email);
        user1.setSex(sex);
        user1.setIntroduce(introduce);
        user1.setCreateTime(createTime);
        user1.setPassword(password);
        user1.setUsername(username);
        user1.setPhone(phone);
        user1.setAddress(user.getAddress().trim());
        return userService.AddUser(user);
    }

    @PostMapping("/SelectAll")
    public Map<String,Object> SelectAll(@RequestParam("PageNum") Integer PageNum, @RequestParam("PageSize") Integer PageSize, @RequestParam("username") String username){
        System.out.println(PageNum);
        System.out.println(PageSize);
        username = '%' + username + '%';
        Map<String,Object> res = new HashMap<String,Object>();
        PageNum = (PageNum-1)*PageSize;

        Integer total = userService.SelectTotal();
        List<User> data = userService.SelectAll(PageNum,PageSize,username);

        res.put("total",total);
        res.put("data",data);
        System.out.println(res);
        return res;

    }

    @DeleteMapping("/DeleteUser/{id}")
    public Object DeleteUser(@PathVariable Integer id){
        JSONObject jsonObject = new JSONObject();

        boolean flag = userService.DeleteUser(id);
        if (flag) {
            jsonObject.put(CONSTS.CODE, 200);
            jsonObject.put(CONSTS.MSG, "删除成功");
        } else {
            jsonObject.put(CONSTS.CODE, 500);
            jsonObject.put(CONSTS.MSG, "删除失败");
        }
        return jsonObject;
    }

    @GetMapping("/SelectById/{id}")
    public User SelectById(@PathVariable Integer id){
        return userService.SelectById(id);
    }

    @PutMapping("/UpdateUser")
    public Object UpdateUser(@RequestBody User user,HttpServletRequest request){

        System.out.println(request.getHeader("token"));
        JSONObject jsonobject = new JSONObject();

        String username = user.getUsername();
        String password = user.getPassword();
        String introduce = user.getIntroduce().trim();
        String image = user.getImage();
        String phone = user.getPhone();
        String email = user.getEmail();
        Date createTime = user.getCreateTime();
        Byte sex = user.getSex();

        User user1 = new User();
        user1.setImage(image);
        user1.setEmail(email);
        user1.setSex(sex);
        user1.setIntroduce(introduce);
        user1.setUpdateTime(createTime);
        user1.setPassword(password);
        user1.setUsername(username);
        user1.setPhone(phone);
        user1.setAddress(user.getAddress().trim());
        user1.setId(user.getId());

        boolean flag = userService.UpdateUser(user1);

        if (flag) {
            jsonobject.put(CONSTS.CODE, 200);
            jsonobject.put(CONSTS.MSG, "更新成功");
        } else {
            jsonobject.put(CONSTS.CODE, 400);
            jsonobject.put(CONSTS.MSG, "更新失败");
        }
        return jsonobject;
    }

    @PostMapping("/login")
    public Object login(@RequestBody User user){
        String username = user.getUsername().trim();
        String password = user.getPassword().trim();
        User user1 = new User();
        JSONObject jsonobject = new JSONObject();
        if(username == ""|| password == "" ){
            jsonobject.put(CONSTS.CODE, 506);
            jsonobject.put(CONSTS.MSG, "参数错误");
        }
        else{
            Result result =(Result) loginService.login(username,password);
            user1 =(User) result.getData();
            if(user1 == null) {
                jsonobject.put(CONSTS.CODE, 506);
                jsonobject.put(CONSTS.MSG, "登陆失败");
            }
            else{
                jsonobject.put(CONSTS.CODE, 200);
                jsonobject.put(CONSTS.MSG, "登陆成功");
                jsonobject.put("data",user1);
                jsonobject.put("token",result.getMsg());
            }
        }

        return jsonobject;
    }

    @GetMapping("/checkToken")
    public boolean checkToken(HttpServletRequest request){
        String token = request.getHeader("token");
        if(token == "" || token == null){
            return false;
        }
        User user = loginService.CheckToken(token);
        System.out.println("user"+user);
        if(user == null){
            System.out.println("%%%%%%%%%%%");
            return false;
        }
        else {
            System.out.println("***********************");
            return true;
        }
    }

    @GetMapping("/logout")
    public Object logout(HttpServletRequest request){
        System.out.println("###################################");
        String token = request.getHeader("token");
        System.out.println("token="+token);
        return loginService.logout(token);
    }

    @GetMapping("/SelectRatio")
    public Object SelectRatio(){

        List<User> users = userService.SelectRatio();
        List<User> adr = userService.SelectAddress();
        if(users.size() == 0){
            return null;
        }
        return users;
    }

    @GetMapping("/SelectUserAll")
    public Integer SelectUserAll(){
        return userService.SelectTotal();
    }
}
